#include "proxy_functionimport.h"
#include "proxy_exports.h"
#include "proxy_local.h"

void InitTrapCalls(void)
{
	trap_Error = (trap_ErrorX)GetProcAddress(proxyCaller, "trap_Error");
	trap_Printf = (trap_PrintfX)GetProcAddress(proxyCaller, "trap_Printf");
	trap_R_RegisterShader = (trap_R_RegisterShaderX)GetProcAddress(proxyCaller, "trap_R_RegisterShader");
	trap_R_RegisterShaderNoMip = (trap_R_RegisterShaderNoMipX)GetProcAddress(proxyCaller, "trap_R_RegisterShaderNoMip");
	trap_S_RegisterSound = (trap_S_RegisterSoundX)GetProcAddress(proxyCaller, "trap_S_RegisterSound");
	trap_S_StartSound = (trap_S_StartSoundX)GetProcAddress(proxyCaller, "trap_S_StartSound");
	CG_Argv = (Argv)GetProcAddress(proxyCaller, "trap_Argv");
	CG_Argv2 = (Argv2)GetProcAddress(proxyCaller, "trap_Argv2");
}

void Attach(void)
{
	proxyCaller = GetModuleHandle("jagamex86.dll");
	jagamex86 = GetModuleHandle("ProxyDLL.dll");
	InitTrapCalls();
	trap_Printf("^4Mod Loaded: ^5Jedi Knight II: ^1Jedi Outcast\n");
	InitHooks();
}

void Detach(void)
{
	int i;
	for(i = 0; i < PHOOK_MAX; i++)
	{
		BG_RemovePatch(&proxyHooks[i]);
	}
	//FreeLibrary(proxyCaller);
}

int __stdcall DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
	switch(dwReason)
	{
		case DLL_PROCESS_ATTACH:
			Attach();
		case DLL_PROCESS_DETACH:
			Detach();
			break;
	}
	return 1;
}